﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponReward : MonoBehaviour
{
    public GameObject[] weapons;
    public GameObject getEff;
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            int randIndex = Random.Range(0, weapons.Length);
            randIndex = 0;
            GameObject randWeapon = weapons[randIndex];
            if (randWeapon)
            {
                //得到玩家对象
                Player player = other.GetComponent<Player>();
                if (player)
                {
                    player.ChangeWeapon(randWeapon);
                }
                //播放奖励特效
                GameObject eff = Instantiate(getEff, this.transform.position, this.transform.rotation);
                //由于该特效身上挂了音效,需要控制音效和音效音量
                AudioSource audioSource = eff.GetComponent<AudioSource>();
                if (audioSource)
                {
                    //根据音乐数据 设置音效大小和是否播放
                    //设置音效大小
                    audioSource.volume = GameDataMgr.Instance.musicData.audioVolume;
                    //音效是否播放
                    audioSource.mute = !GameDataMgr.Instance.musicData.audio;
                    //避免没有勾选 Play on Awake
                    audioSource.Play();
                }
            }
            Destroy(gameObject);
        }
    }
}
